package org.easy.tool.controller;

import org.easy.tool.common.R;
import org.easy.tool.service.IQuestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.LinkedHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Valley
 * @since 2025-02-17 19:58:35
 */
@RestController
@RequestMapping("question")
@CrossOrigin
public class QuestionController {

    @Autowired
    IQuestion questionService;

    @RequestMapping("askAI")
    public R askAI(@RequestBody(required = false)LinkedHashMap data) throws Exception {
        String question = (String) data.get("question");
        return R.ok(questionService.askAI(question));
    }

    // 创建 SSE 连接端点
    @GetMapping("streamData")
    public SseEmitter streamData(@RequestParam String message) {
        return questionService.sseEmit(message);
    }

    @RequestMapping("saveRecord")
    public R saveRecord(@RequestBody LinkedHashMap data) throws Exception {
        boolean result = questionService.saveRecord(data);
        return R.ok(result);
    }

}
